package com.yym.project.view;




import com.yym.project.entity.Book;
import com.yym.project.service.IBookService;
import com.yym.project.service.impl.IBookServiceImpl;
import com.yym.project.utils.Dbutil;

import java.util.List;
import java.util.Scanner;

//视图(发起业务调用)
public class BookSystem {

    //输入工具
   static Scanner sc = new Scanner(System.in);
   static IBookService bookService =  new IBookServiceImpl();
    public static void main(String[] args) {
          welcome();
    }

    public static void welcome(){
        System.out.println("*************************");
        System.out.println("*******欢迎使用xx系统*****");
        System.out.println("*************************");
        System.out.println("选择功能 1. 添加   2.查看  3 删除  4搜索");
        int op =sc.nextInt();
        switch (op){
            case 1: addUI();break; // 调用方法
            case 2: listUI();break;// 调用方法
            case 3: removeUI();break;// 调用方法
            case 4: serachUI();break;// 调用方法
        }
    }

    private static void serachUI() {
        System.out.println("----------搜索--------------------");
        System.out.println("输入书名");
        String bookanme =sc.next();
        try {
            Book book = bookService.selectByName(bookanme);
            if(book!=null) {
                System.out.println(book.getId() + "\t" + book.getBookName() + "\t" + book.getAuthor() + "\t" + book.getPublish());
            }else{
                System.out.println("没有这个书");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        welcome();
    }

    private static void removeUI() {
        System.out.println("----------删除--------------------");
        System.out.println("输入删除id");
        int id =sc.nextInt();
        //找服务
        try {
           int row =  bookService.delete(id);
            System.out.println(row>0?"删除成功":"删除失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
        welcome();
    }

    //列表功能界面
    private static void listUI() {
        //调用查询业务
        try {
            List<Book> list = bookService.list();
            for  ( Book book  :list){
                System.out.println(book.getId()+"\t"+book.getBookName()+"\t"+book.getAuthor()+"\t"+book.getPrice()+"\t"+book.getPublish());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //回到主界面
        welcome();
    }
    //添加功能界面
    private static void addUI() {
        System.out.println("------------------图书录入界面----------------------");
        System.out.println("录入图书");
        String name = sc.next();
        System.out.println("录入作者");
        String author = sc.next();
        System.out.println("录入价格");
        Double price = sc.nextDouble();
        System.out.println("录入时间");
        String date = sc.next();
        // 调用业务
        Book book = new Book( );
        //把输入数据构造成一个对象
        book.setBookName(name);
        book.setAuthor(author);
        book.setPublish(Dbutil.toUtilDate(date));
        book.setPrice(price);
        try {
            int row = bookService.save( book );
            System.out.println(row>0?"成功":"失败");
        } catch (Exception e) {
            e.printStackTrace();
        }
        welcome();
    }

}
